package com.tbit.main.controller;

import com.tbit.main.constant.UserConstant;
import com.tbit.main.controller.interceptor.LoginUserContextHolder;
import com.tbit.main.dao.core.StudentAuthApplyDao;
import com.tbit.main.exception.BaseException;
import com.tbit.main.pojo.AccountUser;
import com.tbit.main.pojo.Result;
import com.tbit.main.pojo.StudentAuthApply;
import com.tbit.manager.api.StudentAuthApplyApi;
import com.tbit.manager.api.dto.StudentAuthApplyDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

/**
 * @Description: 学生认证
 */
@Validated
@RestController
@RequestMapping(value = "/studentAuth")
public class StudentAuthApplyController {

    @Autowired
    private StudentAuthApplyApi studentAuthApplyApi;
    @Autowired
    private StudentAuthApplyDao studentAuthApplyDao;

    /**
     * 审核学生认证
     */
    @GetMapping("/editStudentAuth")
    public Result editStudentAuth(String checkOpinion, @NotBlank String studentIdNo, @NotNull Integer applyId,
                                  @NotNull Integer agree) {
        // 获取用户信息
        AccountUser loginUser = LoginUserContextHolder.getLoginUser();
        StudentAuthApplyDTO studentAuthApplyDTO = new StudentAuthApplyDTO();
        studentAuthApplyDTO.setLoginUserAccountId(loginUser.getAccountId());
        studentAuthApplyDTO.setLoginUserType(loginUser.getAccountUserType());
        studentAuthApplyDTO.setApplyId(applyId);
        studentAuthApplyDTO.setCheckOpinion(checkOpinion);
        studentAuthApplyDTO.setAgree(agree);
        studentAuthApplyDTO.setStudentNO(studentIdNo);

        //权限验证
        StudentAuthApply studentAuthApply = studentAuthApplyDao.getById(applyId);
        if (!UserConstant.ACCOUNT_ADMIN.equals(loginUser.getAccountUserType())) {
            if (studentAuthApply != null && !studentAuthApply.getAccountId().equals(loginUser.getAccountId())) {
                throw new BaseException("System.noPower");
            }
        }
        studentAuthApplyApi.editStudentAuth(studentAuthApplyDTO);

        return Result.success();
    }

    /**
     * 清除学生认证
     *
     * @param userId 用户id
     * @return Result<String>
     */
    @GetMapping("/clearAuth")
    public Result<String> clearAuth(@NotNull Integer userId) {
        AccountUser loginUser = LoginUserContextHolder.getLoginUser();
        if (!UserConstant.ACCOUNT_ADMIN.equals(loginUser.getAccountUserType()) &&
                !UserConstant.ACCOUNT_BRAND.equals(loginUser.getAccountUserType())) {
            throw new BaseException("System.noPower");
        }
        studentAuthApplyApi.clearAuth(userId);
        return Result.success();
    }

    /**
     * 查询学生认证情况
     */
    @GetMapping("/getStudentApplyById")
    public Result<StudentAuthApplyDTO> getStudentApplyById(@NotNull Integer userId) {
        StudentAuthApplyDTO studentApply = studentAuthApplyApi.getStudentApplyById(userId);
        return Result.success(studentApply);
    }

}
